#!/usr/bin/perl
#
# Main component of comparison phase of Yap
# Changed by MJW to create rejects file of those too small to be compared
#
# Perl version before 5 numbered command line args from 0, beginning with
# the first genuine arg (not the command name). Version 5 adopts the UNIX convention



# Change this to match the location of this application
# $BIN='/usr/staff/michaelw/sunbin';
$BIN='.';

if (@ARGV == 1)
    { $ASSNAME=$ARGV[0]; }
elsif (@ARGV == 2)
    { $ASSNAME=$ARGV[0]; $aim_number = $ARGV[1]; }
else
    {print STDERR "Usage: $0 <assignment-pathname> [<threshold number>]\n"; exit 1;}

$ASS="$ASSNAME";

if  (! -d $ASS) {
    print STDERR "There is no directory for assignment $ASSNAME\n";
    exit 1;
}

$olddir = `pwd`;
chdir($ASS);
$[ = 1; 									# change array base to one
$ystatus = "yap.status";
$ysubmissions = "yap.submissions";
$ynumbers = "yap.numbers";
$ysummary = "yap.summary";
$ycutoff = "yap.cutoff";
$ynewfiles = "yap.newfiles";
$ygroups = "yap.groups";
$ytoo_small = "yap.rejects";
$fsize_too_small = 10;		# These ones or smaller John West reject

open(NEWFILES, ">$ynewfiles") ||
	 die "Cannot open file $ynewfiles.\n";


if (( -s $ystatus) && (-s $ysubmissions)) {	# only "yap" the new files
	open(PSTATUS,"<$ystatus")  ||
	 	die "Cannot create file $ystatus.\n";

	open(TEMP,">y$$") ||
	 	die "Cannot create file y$$.\n";

    $two = <PSTATUS>;
    while ($one = <PSTATUS>) {                              # removing last
        print TEMP "$two";
        $two = $one;
    }
    close(PSTATUS);
    close(TEMP);
    rename("y$$", "$ystatus");

	($number,$namelast) = split(/: */,$two);	# number of submissions already tested
	$number--;
	$numpairs = int($number*($number - 1)/2);	# number of pairs tested that are to be kept
	chop($namelast);
	print STDERR "Restarting yap at submission: $two\n";

	open(Y_SUBMISSIONS,"<$ysubmissions") ||
	 	die "Cannot open file $ysumissions.\n";

	while (<Y_SUBMISSIONS>) {				# file holding all the filenames
		if ($. > $number) {
			print NEWFILES;				# file holding new filenames
		}
	}
	if (! $aim_number) {
		$aim_number = 2 * $.;
	}
	close(Y_SUBMISSIONS);

	open (Y_SUMMARY,"<$ysummary") ||
	 	die "Cannot open file $ysummary.\n";

	open (PTEMP,">y$$")  ||
	 		die "Cannot open file y$$.\n";

	while ($_ = <Y_SUMMARY>) {				# remove stats from summary file
		last if /^$/;
		next if /@*${namelast}@*/;
		print PTEMP;
	}
	close(Y_SUMMARY);
	close(PTEMP);
	rename("y$$", "$ysummary");

	open(PNUMBERS,"<$ynumbers")  ||
	 	die "Cannot open file $ynumbers.\n";

	open (PTEMP,">y$$")  ||
	 		die "Cannot open file y$$.\n";

	while ($_ = <PNUMBERS>) {
		last if ($. > $numpairs);
		print PTEMP;
	}
	close(PTEMP);
	close(PNUMBERS);
	rename("y$$", "$ynumbers");
}
else {
    if ( ! -s $ysubmissions ) {				# remake submissions file and "yap" everything
		open(Y_SUBMISSIONS,">$ysubmissions") ||
	 		die "Cannot open file $ysubmissions.\n";

		open(TESTS,"find adjusted ontime late tempdir -name '*tokens' -print|") ||
	 		die 'Cannot open find pipe.';
		open(Y_REJECTS, ">$ytoo_small") ||
                        die "Cannot open $ytoo_small";

		while ($_ = <TESTS>)
		    {
		    open(STUD_FILE, "<$_") ||
			die "Cannot open $_\n";
		    $stud_file_size = <STUD_FILE>;	#size is first item
		    if ($stud_file_size <= $fsize_too_small)
			{
			print Y_REJECTS;
			}
		    else
			{
			print Y_SUBMISSIONS;
			print NEWFILES;
			}
		    close(STUD_FILE);
		    }
		close(Y_REJECTS);
		if (! $aim_number) {
			$aim_number = 2 * $.;
		}

		close(TESTS);
		close(Y_SUBMISSIONS);
	}
	else {
		open (Y_SUBMISSIONS,"<$ysubmissions")  ||
	 		die "Cannot open file $ysubmissions.\n";
		while (<Y_SUBMISSIONS>) {
			print NEWFILES;					# "yap" everything again
		}
		if (! $aim_number) {
			$aim_number = 2 * $.;
		}

		close(Y_SUBMISSIONS);
	}

	$number = 0;
}

close(NEWFILES);
$sort_command = "sort -n -r +2 -3 $ysummary | head -$aim_number > y$$ ; mv y$$ $ysummary";

system "$BIN/rkr_gst", "2", "$sort_command", "$number", "$aim_number";
# system "cat", "$ysummary";
unlink 'yap.newfiles';

&Group($aim_number);



$n = 0;
$sumx = 0;
open(PNUMBERS,"<$ynumbers")  ||
	 die "Cannot open file $ynumbers.\n";

while ($_ = <PNUMBERS>) {
	($first)=split(/  */,$_);
	$sumx += $first;
    $sumxsqr += $first**2;
    $n++;
}
close PNUMBERS;

open(Y_SUMMARY,">>$ysummary")  ||
	 	die "Cannot open file $ysummary.\n";

if ($n > 1) {
	printf(Y_SUMMARY "\n\nMean match %6.2f\n", $sumx / $n);
	printf(Y_SUMMARY "Standard deviation of matches %6.2f\n",sqrt(($sumxsqr - $sumx * $sumx / $n)  / ($n - 1)));
}
else {
	print Y_SUMMARY "Not enough submissions for statisical analysis\n";
}
close Y_SUMMARY;
chdir(olddir);

sub Group {					# print groups into file yap.groups
	($aim_number) = @_;
	open(Y_SUMMARY,"<$ysummary")  ||
	 	die "Cannot open file $ysummary.\n";

	$No_Groups = 0;
	$#groups = 200;
	$numlines = 0;
	$gcounter = 1000;

	# put all the submission names in a chain pointed to by the "groups" array
	while($line = <Y_SUMMARY>) {
		if (++$numlines <= $aim_number) {
			($first,$second,$match,$superset1,$superset2) = split(' ',$line);

			if (($submissions{$first} =~ /../) && ($submissions{$second} =~ /../)) {
				$temp1 = $first;							# two chains need to be joined
				while ($submissions{$temp1} =~ /\D/) {
					$temp1 = $submissions{$temp1};
				}
				$grp1 = $submissions{$temp1};
				$temp2 = $second;
				while ($submissions{$temp2} =~ /\D/) {
					$temp2 = $submissions{$temp2};
				}
				$grp2 = $submissions{$temp2};
				if ($grp1 < $grp2) {
					$submissions{$temp1} = $groups{$grp2};
					$submissions{$temp2} = $grp1;
					$groups{$grp2} = '';
				}
				elsif ($grp2 > $grp1) {
					$submissions{$temp1} = $groups{$grp1};
					$submissions{$temp2} = $grp2;
					$groups{$grp1} = '';
				}
			}
			elsif ($submissions{$first} =~ /../) {			# place in chain
				$temp = $first;
				while ($submissions{$temp} =~ /\D/) {
					$temp = $submissions{$temp};
				}
				$grp = $submissions{$temp};
				$submissions{$temp} = $second;
				$submissions{$second} = $grp;
			}
			elsif ($submissions{$second} =~ /../) {			# place in chain
				$temp = $second;

				while ($submissions{$temp} =~ /\D/) {
					$temp = $submissions{$temp};
				}
				$grp = $submissions{$temp};
				$submissions{$temp} = $first;
				$submissions{$first} = $grp;
			}
			else {											# create chain
				$gcounter++;
				$groups{"$gcounter"} = $first;
				$submissions{$first} = $second;
				$submissions{$second} = "$gcounter";
			}
		}
	}
	close Y_SUMMARY;


	$No_Groups = 0;
	foreach $group (sort keys(%groups)) {
		if ($groups{$group} ne '') {
			$No_Groups++;
			$temp = $groups{$group};
			while ($submissions{$temp} =~ /\D/) {
					$which_group{$temp} = $No_Groups;
				$temp = $submissions{$temp};
			}
				$which_group{$temp} = $No_Groups;
		}
	}

	open(Y_SUMMARY,"<$ysummary")  ||
 		die "Cannot open file ysummary.\n";

	# Place all the matches from a summary file in a groups string (=array)

	$#new_groups = $No_Groups;
	$counter = 0;
	while ((++$counter <= $aim_number) && ($line = <Y_SUMMARY>)) {
		($first,$second,$match,$superset1,$superset2) = split(' ',$line);
		if ($new_groups[$which_group{$first}]) {
			$new_groups[$which_group{$first}] = join(';;',$new_groups[$which_group{$first}],$line);
		}
		else {
			$new_groups[$which_group{$first}] = $line;
		}
	}


	close Y_SUMMARY;

	open(Y_GROUPS,">$ygroups") ||
	 	die "Cannot open file $ygroups\n";

	for ($numgroups = 1; $numgroups <= $No_Groups; $numgroups++) {
		printf(Y_GROUPS "\n\n               Group Number: %d\n",$numgroups);
		foreach $sub (keys (%which_group)) {
			if ($which_group{$sub} == $numgroups) {
				$sub2 = join('',split(/\.tokens/,$sub));
				print Y_GROUPS "    $sub2\n";
			}
		}
		print Y_GROUPS "\n";

		foreach $temp (split(/;;/,$new_groups[$numgroups])) {
			chop $temp;
			($temp1,$temp2,$temp5,$temp6,$temp7,$extra) = split(/ +/,$temp);
			($temp3) = split(/\.tokens/,$temp1);
			($temp4) = split(/\.tokens/,$temp2);

			printf(Y_GROUPS "%-20s  %-20s  %-3d %s %s\n",$temp3,$temp4,$temp5,$temp6,$temp7);
		}
	}

	close Y_GROUPS;
}

